package com.ten.mall.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ten.mall.vo.weixin.Pay;

public class PayUtil {
	private static Logger log = LoggerFactory.getLogger(PayUtil.class);
	public static  String getCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new        
        SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}

	 public static  String getRandomString(int length){  
	        Random random = new Random();  
	          
	        StringBuffer sb = new StringBuffer();  
	          
	        for(int i = 0; i < length; ++i){  
	            int number = random.nextInt(3);  
	            long result = 0;  
	            switch(number){  
	            case 0:  
	                result = Math.round(Math.random() * 25 + 65);  
	                sb.append(String.valueOf((char)result));  
	                break;  
	            case 1:  
	                result = Math.round(Math.random() * 25 + 97);  
	                sb.append(String.valueOf((char)result));  
	                break;  
	            case 2:  
	                sb.append(String.valueOf(new Random().nextInt(10)));  
	                break;  
	            }  
	        }  
	        
	        return sb.toString();     
	    }  
	
	
	
	 public static  String getTempStamp() {
		    Date date=new Date();	
			long a= date.getTime()/1000;
			String TempStamp=String.valueOf(a);
			return TempStamp;
		}
	 
	 public  static Integer changeToFen(BigDecimal decimal){
		 BigDecimal c=new BigDecimal(100.00);
		 BigDecimal temp =decimal.multiply(c);
		 Integer b =temp.intValue();
		 return b;
	 }
	 /**
	  * 请求值转换为xml格式 SortedMap转xml
	  * @param params
	  * @return
	  */
	 public static String SortedMaptoXml(SortedMap<Object,Object> params) {
	        StringBuilder sb = new StringBuilder();
	        Set es = params.entrySet();
	        Iterator it = es.iterator();
	        sb.append("<xml>\n");
	        while(it.hasNext()) {
	            Map.Entry entry = (Map.Entry)it.next();
	            String k = (String)entry.getKey();
	            Object v = entry.getValue();
	            sb.append("<"+k+">");
	            sb.append(v);
	            sb.append("</"+k+">\n");
	        }
	        sb.append("</xml>");
	        return sb.toString();
	    }
	 /**
	  * 产生 退款id
	  * @param orderId
	  * @return
	  */
	 public static String createRefuseId(String orderId){
		 String rs =orderId.replace("H", "D");
		 return rs;
	 }
	 /**
	  * 获取当前时间的月份
	  * @return
	  */
	 public static Integer getMonth(){
		 Calendar cal = Calendar.getInstance();
		 int month = cal.get(Calendar.MONTH) + 1;
		 return month;
	 }
	 
	public static String queryPay(Pay pay,String key) throws Exception{
		String url="https://api.mch.weixin.qq.com/pay/orderquery";
		String nonceStr = PayUtil.getRandomString(20);
        SortedMap<Object,Object> parameters = new TreeMap<>();
        parameters.put("appid", pay.getAppid());
        parameters.put("mch_id", pay.getMch_id());
        parameters.put("nonce_str",nonceStr);
        parameters.put("out_trade_no", pay.getOut_trade_no());
        String sign =Sign.creatSign(parameters,key);
        parameters.put("sign",sign);
        String data =SortedMaptoXml(parameters);
        log.info("query:"+data);
        String respxml = Httpsrequest.HttpsRequest(url, "POST", data);
        log.info("respxml:"+respxml);
		return respxml;
	}
}
